package cn.cmvideo.springboot.util;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;


public class XML2Json
{
	public static void main(String[] args)
	{
		InputStream is = XML2Json.class.getResourceAsStream("851.xml");
		String xml = "";
		try
		{
			xml = IOUtils.toString(is,"UTF-8");
			
			// 不带根标签
//			XMLSerializer xmlSerializer = new XMLSerializer();
//			JSON json = xmlSerializer.read(xml);
//			
//			JSONObject object = JSONObject.fromObject(json);
//			String contid = object.getString("contid");
//			System.out.println(contid);
//			System.out.println(json);
			
			// 带有根标签
			JSONObject jsonObject = XML.toJSONObject(xml);
			System.out.println("object2 = " + jsonObject.toString());
			
			String contid2 = jsonObject.getJSONObject("content").getString("contid");
			System.out.println("contid2 = "+ contid2);
			
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (JSONException e)
		{
			e.printStackTrace();
		}   
	}
}
